package com.ruoyi.common.json;


import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class JsonList extends ArrayList<Object> {


    public <T> List<T> toJavaList(Class<T> clazz) {
        List<T> list = new ArrayList<>(this.size());
        Iterator var4 = this.iterator();
        while (var4.hasNext()) {
            Object item = var4.next();
            T classItem = JsonUtil.toJavaObject(item, clazz);
            list.add(classItem);
        }
        return list;
    }

    public String getString(int key) {
        return JSONObject.valueAsStr(this.get(key));
    }
    public Long getLong(int key) {
        return JSONObject.valueAsLong(this.get(key));
    }

    public Boolean getBool(int key) {
        return JSONObject.valueAsBool(this.get(key));
    }

    public Integer getInt(int key) {
        return JSONObject.valueAsInt(this.get(key));
    }

    public long getLongVal(int key) {
        Long aLong = getLong(key);
        return aLong == null ? 0L : aLong;
    }

    public int getIntVal(int key) {
        Integer integer = getInt(key);
        return integer == null ? 0 : integer;
    }

    public boolean getBooleanVal(int index) {
        Boolean value = this.getBool(index);
        return value != null && value;
    }


    public <T> T getObject(int index, Class<T> clazz) {
        Object obj = this.get(index);
        return JsonUtil.toJavaObject(obj, clazz);
    }


    public JsonMap getJsonMap(int index) {
        Object value = this.get(index);
//        if (value instanceof Map) {
//            return (JsonMap) value;
//        } else {
//            return JsonUtil.toJsonMap(value);
//        }
        return JsonUtil.toJsonMap(value);
    }

    public JsonList getJsonList(int index) {
        Object value = this.get(index);
        return JsonUtil.toJsonList(value);
    }


    @Override
    public String toString() {
        return JsonUtil.toJSONString(this);
    }


}
